package com.fieldchain.ex.fa100.ui.money.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.fieldchain.ex.fa100.R;
import com.fieldchain.ex.fa100.bean.BillBean;
import com.bumptech.glide.Glide;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by biyunlong
 * Email: 1225198434qq.com
 * Data:  2018/9/13
 */
public class BillAdapter extends RecyclerView.Adapter<BillAdapter.ViewHolder> {
    private Context mContext;
    private String[] type = {"", "买入", "卖出", "", "冻结", "解冻"};
    private String[] tags = new String[30];

//    操作类型：1=交易中心撮合交易,2=交易挂单,5=撤单,6=差价退回,12=充值,13=提现,15=推荐赠送糖果,16=注册赠送糖果,17=实名赠送糖果,18=抽奖奖励,19=注册实名推荐赠送糖果解冻,20=平台奖励,28=PUSH交易

    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public BillAdapter(Context mContext) {
        this.mContext = mContext;
        tags[1] = "";
    }

    private List<BillBean> list = new ArrayList<>();

    public void setData(List<BillBean> list) {
        this.list.clear();
        this.list.addAll(list);

        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_bill, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        BillBean bean = list.get(position);

        String add = "+";
        String status = "";
        String market="";
        if (bean.getOperation() == 1) {
            //交易
            if (bean.getDealType() == 1) {
                status = "买入";
                add = "+";
            } else if (bean.getDealType() == 2) {
                status = "卖出";
                add = "-";
            } else if (bean.getDealType() == 4) {
                status = "冻结";
                add = "-";
            } else if (bean.getDealType() == 5) {
                status = "解冻";
                add = "+";
            }
        } else if (bean.getOperation() == 2) {
            //挂单
            status = "挂单";
            add = "-";
        } else if (bean.getOperation() == 5) {
            //撤单
            status = "撤单";
            add = "+";

        } else if (bean.getOperation() == 6) {
            //差价退回
            status = "差价退回";
            add = "+";

        } else if (bean.getOperation() == 12) {
            //充值
            status = "充值";
            add = "+";

        } else if (bean.getOperation() == 13) {
            //提币
            status = "提币";
            add = "-";

        } else if (bean.getOperation() == 15) {
            //推荐赠送糖果
            status = "推荐赠送";
            add = "+";

        } else if (bean.getOperation() == 16) {
            //注册赠送糖果
            status = "注册赠送";
            add = "+";

        } else if (bean.getOperation() == 17) {
            //实名赠送糖果
            status = "实名赠送";
            add = "+";

        } else if (bean.getOperation() == 18) {
            //抽奖奖励
            status = "抽奖奖励";
            add = "+";

        } else if (bean.getOperation() == 19) {
            //注册实名推荐赠送的糖果解冻
            status = "解冻";
            add = "+";

        } else if (bean.getOperation() == 20) {
            //平台奖励
            status = "平台奖励";
            add = "+";

        } else if (bean.getOperation() == 28) {
            //PUSH交易
            status = "PUSH交易";
            add = "+";
        }
        holder.tvType.setText(status);
        String url=bean.getUrl();

        Glide.with(mContext).load(bean.getUrl()).into(holder.ivCoin);
        holder.tvCompany0.setText(bean.getTransactionCurrency().toUpperCase());
        holder.tvCompany1.setText(bean.getTransactionCurrency().toUpperCase());
        holder.tvNum0.setText(add + bean.getDealTotal() + "");
        holder.tvTime.setText(sdf.format(bean.getCreateTime()));

        if(bean.getOperation()==1||bean.getOperation()==2){
            holder.tvCompany0.setText(bean.getMarket().toUpperCase().replace("_","/"));
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView ivCoin;
        private TextView tvType;
        private TextView tvCompany0;
        private TextView tvNum0;
        private TextView tvCompany1;
        private TextView tvTime;

        public ViewHolder(View itemView) {
            super(itemView);
            ivCoin = itemView.findViewById(R.id.iv_coin);
            tvType = itemView.findViewById(R.id.tv_type);
            tvCompany0 = itemView.findViewById(R.id.tv_company0);
            tvNum0 = itemView.findViewById(R.id.tv_num0);
            tvCompany1 = itemView.findViewById(R.id.tv_company1);
            tvTime = itemView.findViewById(R.id.tv_time);
        }
    }
}
